<template>
  <div id="blog_all">
    <!-- <h2>
        <div>{{ title }}</div>
        <div class="add">
          <button @click="updateShow1 = !updateShow1">添加</button> <button>筛选</button>
        </div>
      </h2> -->
    <header>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"> homepage </el-breadcrumb-item>
        <el-breadcrumb-item>{{ title }}</el-breadcrumb-item>
      </el-breadcrumb>
      <div style="display: flex">
        <div class="icon" @click="updateShow1 = true">添加</div>
        <div class="icon" @click="handleSelectClose">筛选</div>
      </div>
    </header>
    <!-- 筛选 -->
    <div class="select" ref="select">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="公告标题">
              <el-input v-model="formInline.title" placeholder="公告标题" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="formSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="main">
      <div class="table-container">
        <table border="1">
          <thead>
            <tr>
              <th>公告标题</th>
              <!-- <th>段落1</th>
              <th>段落2</th> -->
              <th>发布日期</th>
              <th>更新时间</th>
              <th style="text-align: center">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="post in newsdata" :key="post._id">
              <td>{{ post.title }}</td>
              <!-- <td>{{ post.p_1 }}</td>
              <td>{{ post.p_2 }}</td> -->
              <td>{{ post.publishedAt }}</td>
              <td>{{ post.uptime }}</td>
              <td class="btntd">
                <div class="btn">
                  <button class="one iconfont icon-xiugai" @click="modify(post._id)"></button>
                  <button class="del iconfont icon-shanchu" @click="del(post._id)"></button>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 修改 -->
    <el-drawer
      v-model="updateShow2"
      title="修改公告"
      :direction="direction"
      size="50%"
      :before-close="handleClose2"
    >
      <UpdateBlog
        @handleDraewr="handleDrawer"
        :filterNew="filterNew"
        :key="filterNew?._id"
      ></UpdateBlog>
    </el-drawer>
    <!-- 添加 -->
    <el-drawer
      v-model="updateShow1"
      title="添加公告"
      :direction="direction"
      size="50%"
      :before-close="handleClose"
    >
      <UpdateBlog @handleDraewr="handleDrawer"></UpdateBlog>
    </el-drawer>
  </div>
</template>

<script setup>
import { News, delBlog, selectnews } from '@/utils/zyy-axios'
import UpdateBlog from '@/components/news/UpdateBlog.vue'
import { ref, useTemplateRef } from 'vue'
import { ElMessageBox } from 'element-plus'

const updateShow1 = ref(false)
const updateShow2 = ref(false)
const filterNew = ref(false)
const direction = ref('rtl')

const newsdata = ref([])

News().then((res) => {
  newsdata.value = res.data.newsResult
  console.log('thisnews', newsdata.value)
})

const handleDrawer = () => {
  updateShow1.value = false
  updateShow2.value = false
  News().then((res) => {
    newsdata.value = res.data.newsResult
  })
}
const modify = (id) => {
  updateShow2.value = true
  filterNew.value = newsdata.value.find((item) => item._id == id)
  console.log('修改', filterNew.value)
}
const handleClose = (done) => {
  ElMessageBox.confirm('公告还未添加，是否退出?')
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
// 修改NEWS
const handleClose2 = (done) => {
  ElMessageBox.confirm('公告还未修改，是否退出?')
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
const del = async (id) => {
  await delBlog(id)
  News().then((res) => {
    newsdata.value = res.data.newsResult
  })
  console.log('删除', id)
}

// 表格标题
const title = '公告列表'

// 筛选
const select = useTemplateRef('select')
const flag = ref(false)
const handleSelectClose = () => {
  flag.value = !flag.value
  if (flag.value) {
    select.value.style.height = '80rem'
    select.value.style.visibility = 'inherit'
    select.value.style.opacity = '1'
  } else {
    select.value.style.height = '0'
    select.value.style.visibility = 'hidden'
    select.value.style.opacity = '0'
  }
}

const formInline = ref({
  title: '',
  au: '',
  phone: '',
})

const formSubmit = () => {
  const { title, au, phone } = formInline.value
  selectnews(title, au, phone).then((res) => {
    newsdata.value = res.data.list
    console.log(res.data.list)

    // total.value = res.data
  })
}
</script>


<style scoped lang="less">
@import url(../../css/icon.css);
@theme-color: #409eff;

header {
  padding: 10rem 30rem 0;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .icon {
    padding: 5rem 10rem;
    background-color: #409eff;
    color: #fff;
    border-radius: 4rem;
    margin-left: 8rem;
    font-size: 12rem;
  }
}

.select {
  height: 0;
  visibility: hidden;
  opacity: 0;
  margin: 8rem 20rem;
  padding: 0 20rem;
  background-color: #fff;
  border-radius: 10rem;
  transition: all 1s;
}

#blog_all {
  width: 100%;
}

.main {
  z-index: 99;
  @theme-color: #f8f9fa;

  .table-container {
    margin: 20rem;
    font-family: 'Arial', sans-serif;
  }

  h2 {
    color: @theme-color;
    font-size: 24rem;
    margin-bottom: 18rem;
    display: flex;
    justify-content: space-between;

    .add {
      font-size: 20rem;

      button {
        padding: 6rem 12rem;
        border: none;
        cursor: pointer;
        border-radius: 4rem;
        font-size: 18rem;
        transition: background-color 0.3s;
        background-color: #409eff;
        color: white;
      }
    }
  }

  table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10rem;
    background-color: #fff;
    box-shadow: 0 4rem 10rem rgba(0, 0, 0, 0.05);
    border-radius: 8rem;
    overflow: hidden;
  }

  th,
  td {
    padding: 6rem 15rem;
    font-size: 14rem;
    text-align: left;
    border: 1rem solid #e0e0e0;
  }

  .btntd {
    padding: 6rem 0;
  }

  th {
    background-color: @theme-color;
    color: #9c9c9c;
    font-weight: 600;
    font-size: 14rem;
  }

  tbody tr:nth-child(even) {
    background-color: #f7f7f7;
  }

  tbody tr:hover {
    background-color: #f1f1f1;
  }

  tbody {
    .btn {
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: space-evenly;

      .del {
        background-color: #ff4d4f;
        color: white;
      }

      .one {
        // background-color: #16ceaf;
        color: rgb(0, 0, 0);
      }
    }
  }

  .edit-btn {
    background-color: @theme-color;
    color: #fff;
    margin-right: 8rem;

    &:hover {
      background-color: darken(@theme-color, 10%);
    }
  }

  .delete-btn {
    background-color: #ff4d4f;
    color: #fff;

    &:hover {
      background-color: darken(#ff4d4f, 10%);
    }
  }
}

.pagination {
  width: 96%;
  display: flex;
  padding: 0 20rem;
  justify-content: end;
}

.el-form--inline .el-form-item {
  width: 100%;
}

.el-button,
.el-button.is-round {
  margin: auto;
  padding: 0 26rem;
}

.el-form {
  padding-top: 15rem;
}
</style>